<?php
/**
 * Created by PhpStorm.
 * User: wangsentao
 * Date: 2018/3/7
 * Time: 下午5:59
 */

namespace app\api\repository;

use app\common\Service;

class OrderInfoShaohuoRepository extends Service
{

    /**
     * 更新支付订单 记录状态
     */
    public function updateOrderPayedStatus($order_num = null, $order_id = null,$trade_no = '')
    {
        if ($order_num == null && $order_id == null) {
            return [
                'code' => '100031',
                'msg' => '订单号或订单ID只允许一个为空',
            ];
        }
        $model = model("app\common\Model\OrderInfoShaohuo");

        if ($order_num == null) {
            $model->where('order_id', $order_id);
        }

        if ($order_id == null) {
            $model->where('order_num', $order_num);
        }

        if ($trade_no != '') {
            $model->trade_no = $trade_no;
        }

        $model->status = 2;

        $res = $model->isUpdate(true)->save();

        return [
            'code' => '000000',
            'data' => $res,
        ];
    }

    /**
     * 根据订单号获取订单信息
     */
    public function getOrderInfoById($order_id)
    {
        if (empty($order_id)) {
            return [
                'code' => '100031',
                'msg' => '订单id不能为空',
            ];
        }
        $model = model("app\common\Model\OrderInfoShaohuo");
        $res = $model
            ->field(
                'uid,
                      order_num,
                      order_id,
                      fc_id,
                      item_name,
                      status,
                      shipping_fee,
                      trade_no')
            ->where('order_id',$order_id)
            ->find();
        return [
            'code' => '000000',
            'data' => $res->getData(),
        ];
    }
}